﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget* parent)
	: QMainWindow(parent)
	, ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	moveCanvasToCenter();
	ui->m_shapeBorderWidth->installEventFilter(this);
	ui->m_shapeBorderStyle->installEventFilter(this);

	connect(ui->btnSelect, &QToolButton::clicked, this, &MainWindow::onSelectButtonClicked);
	connect(ui->btnLine, &QToolButton::clicked, this, &MainWindow::onLineButtonClicked);
	connect(ui->btnCircle, &QToolButton::clicked, this, &MainWindow::onCircleButtonClicked);
	connect(ui->btnRectangle, &QToolButton::clicked, this, &MainWindow::onRectButtonClicked);
	connect(ui->btnPentagon, &QToolButton::clicked, this, &MainWindow::onPentagonButtonClicked);
	connect(ui->btnHexagon, &QToolButton::clicked, this, &MainWindow::onHexagonButtonClicked);
	connect(ui->btnPentagram, &QToolButton::clicked, this, &MainWindow::onPentagramButtonClicked);

	connect(ui->m_canvasWidth, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::onSetCanvasWidth);
	connect(ui->m_canvasHeight, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::onSetCanvasHeight);
	connect(ui->m_canvasZoom, QOverload<qreal>::of(&QDoubleSpinBox::valueChanged), this, &MainWindow::onZoomCanvas);
	connect(ui->m_setCanvasColorButton, &QPushButton::pressed, this, &MainWindow::setCanvasColor);

	connect(ui->m_shapeBorderWidth, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::onSetShapeBorderWidth);
	connect(ui->m_shapeBorderStyle, &BorderStyleButton::setShapePen, this, &MainWindow::onSetShapeBorderStyle);
	connect(ui->m_setShapeBorderColorButton, &QPushButton::pressed, this, &MainWindow::onSetShapeBorderColor);
	connect(ui->m_setShapeColorButton, &QPushButton::pressed, this, &MainWindow::onSetShapeColor);
	connect(ui->m_canvas, &Canvas::currentShapeBorderWidth, this, &MainWindow::onSetBorderWidthSpinBoxValue);
	connect(ui->m_canvas, &Canvas::currentShapeBorderStyle, ui->m_shapeBorderStyle, &BorderStyleButton::setComBoxView);
	connect(ui->m_canvas, &Canvas::currntShapeBorderColor, this, &MainWindow::onSetShapeBorderColorButtonColor);
	connect(ui->m_canvas, &Canvas::currntShapeColor, this, &MainWindow::onSetShapeColorButtonColor);
	connect(ui->m_canvas, &Canvas::shapeSelected, this, &MainWindow::onIsSelectShape);
}

MainWindow::~MainWindow()
{
	delete ui;
}

void MainWindow::moveCanvasToCenter()
{
	int x, y;
	x = (ui->scrollArea->width() - ui->m_canvas->width()) / 2;
	y = (ui->scrollArea->height() - ui->m_canvas->height()) / 2;
	ui->m_canvas->move(x, y);
	ui->m_canvas->update();
}

void MainWindow::onCleanCanvasClicked()
{
	QMessageBox::StandardButton cleanReply;
	cleanReply = QMessageBox::question(this, QStringLiteral("清空画布"), QStringLiteral("确定要清空画布吗"),
		QMessageBox::Yes | QMessageBox::No);
	if (cleanReply == QMessageBox::Yes)
	{
		QMessageBox::StandardButton savePNGReply;
		savePNGReply = QMessageBox::question(this, QStringLiteral("导出PNG"), QStringLiteral("需要导出PNG文件吗"),
			QMessageBox::Yes | QMessageBox::No);
		if (savePNGReply == QMessageBox::Yes)
		{
			onExportPNGBtnClicked();
		}
		ui->m_canvas->cleanCanvas();
	}
}

void MainWindow::onExportPNGBtnClicked()
{
	QString fileName = QFileDialog::getSaveFileName(this, QStringLiteral("导出PNG"), "", "PNG Files (*.png)");
	if (!fileName.isEmpty())
	{
		QPixmap pixmap(ui->m_canvas->size());
		ui->m_canvas->render(&pixmap);
		if (pixmap.save(fileName))
		{
			QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("导出PNG成功，路径为") + fileName);
		}
		else
		{
			QMessageBox::warning(this, QStringLiteral("错误"), QStringLiteral("导出失败，请重试"));
		}
	}
}

void MainWindow::onSelectButtonClicked()
{
	ShapeParameter::getInstance()->setDrawFlag(Canvas::DrawFlag::MouseDrawFlag);
}

void MainWindow::onLineButtonClicked()
{
	ShapeParameter::getInstance()->setDrawFlag(Canvas::DrawFlag::LineDrawFlag);
}

void MainWindow::onCircleButtonClicked()
{
	ShapeParameter::getInstance()->setDrawFlag(Canvas::DrawFlag::CircleDrawFlag);
}

void MainWindow::onRectButtonClicked()
{
	ShapeParameter::getInstance()->setDrawFlag(Canvas::DrawFlag::RectDrawFlag);
}

void MainWindow::onPentagonButtonClicked()
{
	ShapeParameter::getInstance()->setDrawFlag(Canvas::DrawFlag::PentagonDrawFlag);
}

void MainWindow::onHexagonButtonClicked()
{
	ShapeParameter::getInstance()->setDrawFlag(Canvas::DrawFlag::HexagonDrawFlag);
}

void MainWindow::onPentagramButtonClicked()
{
	ShapeParameter::getInstance()->setDrawFlag(Canvas::DrawFlag::PentagramDrawFlag);
}

void MainWindow::onSetCanvasWidth(int width)
{
	ui->m_canvas->setCanvasWidth(width);
}

void MainWindow::onSetCanvasHeight(int height)
{
	ui->m_canvas->setCanvasHeight(height);
}

void MainWindow::onZoomCanvas(qreal scale)
{
	ShapeParameter::getInstance()->setShapeScale(scale);
	ShapeParameter::getInstance()->setCanvasScale(scale);
	ui->m_canvas->zoomCanvas(scale);
}

void MainWindow::setCanvasColor()
{
	QRgb color = selectColor();
	QString qssStr = QString("background: #%1;").arg(QString::number(color, 16));
	ui->m_canvas->setStyleSheet(qssStr);
	qssStr += "border:none;";
	ui->m_setCanvasColorButton->setStyleSheet(qssStr);
}

void MainWindow::onSetShapeBorderWidth(int width)
{
	ShapeParameter::getInstance()->setBorderWidth(width);
	ui->m_canvas->update();
}

void MainWindow::onSetBorderWidthSpinBoxValue(int width)
{
	ui->m_shapeBorderWidth->setValue(width);
}

void MainWindow::onSetShapeBorderStyle(Qt::PenStyle style)
{
	ShapeParameter::getInstance()->setBorderStyle(style);
	ui->m_canvas->update();
}

void MainWindow::onSetShapeBorderColor()
{
	if (m_isSelectShape)
	{
		QRgb color = selectColor();
		ShapeParameter::getInstance()->setBorderColor(color);
		onSetShapeBorderColorButtonColor(color);
		ui->m_canvas->update();
	}
	else
	{
		QMessageBox::warning(this, QStringLiteral("警告"), QStringLiteral("未选中图形，请先选择图形"));
	}
}

void MainWindow::onSetShapeBorderColorButtonColor(QRgb color)
{
	QString qssStr = QString("background: #%1;border:none;").arg(QString::number(color, 16));
	ui->m_setShapeBorderColorButton->setStyleSheet(qssStr);
}

void MainWindow::onSetShapeColor()
{
	if (m_isSelectShape)
	{
		if (ShapeParameter::getInstance()->getCurrentShape()->getShapeType() == ShapeType::LineShapeType)
		{
			QMessageBox::warning(this, QStringLiteral("警告"), QStringLiteral("当前绘制的是直线，只能设置边框颜色"));
		}
		else
		{
			QRgb color = selectColor();
			ShapeParameter::getInstance()->setShapeColor(color);
			onSetShapeColorButtonColor(color);
			ui->m_canvas->update();
		}
	}
	else
	{
		QMessageBox::warning(this, QStringLiteral("警告"), QStringLiteral("未选中图形，请先选择图形"));
	}
}

void MainWindow::onSetShapeColorButtonColor(QRgb color)
{
	QString qssStr = QString("background: #%1;border:none;").arg(QString::number(color, 16));
	ui->m_setShapeColorButton->setStyleSheet(qssStr);
}

void MainWindow::onIsSelectShape(bool isSelected)
{
	m_isSelectShape = isSelected;
	if (isSelected)
	{
		ui->m_shapeBorderWidth->setEnabled(true);
		ui->m_shapeBorderStyle->setEnabled(true);
	}
	else
	{
		ui->m_shapeBorderWidth->setEnabled(false);
		ui->m_shapeBorderStyle->setEnabled(false);
	}
}

QRgb MainWindow::selectColor()
{
	QColorDialog colorDialog(this);
	QColor color = colorDialog.getColor();
	if (!color.isValid())
	{
		return qRgb(255, 255, 255);
	}
	QRgb rgb = qRgb(color.red(), color.green(), color.blue());
	return rgb;
}

bool MainWindow::eventFilter(QObject* watched, QEvent* event)
{
	if (watched == ui->m_shapeBorderWidth &&
		(event->type() == QEvent::MouseButtonPress ||
			event->type() == QEvent::Wheel))
	{
		QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
		if (!ui->m_shapeBorderWidth->isEnabled())
		{
			QMessageBox::warning(this, QStringLiteral("警告"), QStringLiteral("未选中图形，请先选择图形"));
			mouseEvent->ignore();
			return true;
		}
	}
	else if (watched == ui->m_shapeBorderStyle && event->type() == QEvent::MouseButtonPress)
	{
		QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
		if (!ui->m_shapeBorderStyle->isEnabled())
		{
			QMessageBox::warning(this, QStringLiteral("警告"), QStringLiteral("未选中图形，请先选择图形"));
			mouseEvent->ignore();
			return true;
		}
	}
	return QWidget::eventFilter(watched, event);
}